// FrogSortDesktopDialog.cpp : Implementation of CFrogSortDesktopDialog

#include "stdafx.h"
#include "FrogSortDesktopDialog.h"
#include "FrogMessageBox.h"
#include <atldlgs.h>
#include <math.h>

DesktopSortConfig CFrogSortDesktopDialog::s_DesktopSortConfig;

// CFrogSortDesktopDialog
CFrogSortDesktopDialog::CFrogSortDesktopDialog(HICON hIcon):
CFrogBaseDialog(hIcon),
m_UseExistingWallaper(false)
{
	m_UseExistingWallaper = s_DesktopSortConfig.useExistingWallaper;
}

LRESULT CFrogSortDesktopDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);

	m_SliderOpacityGroup.Attach(GetDlgItem(IDC_SLIDER_OPACITY_1));
	m_SliderOpacityBorder.Attach(GetDlgItem(IDC_SLIDER_OPACITY_2));

	m_ColorBorderBtn.SubclassWindow(GetDlgItem(IDC_BUTTON_COLOR_BORDER));
	m_ColorBorderBtn.SetColor(s_DesktopSortConfig.borderColor.ToCOLORREF());

	m_ColorBtn1.SubclassWindow(GetDlgItem(IDC_BUTTON_COLOR_1));
	m_ColorBtn2.SubclassWindow(GetDlgItem(IDC_BUTTON_COLOR_2));

	m_ColorBtn1.SetColor(s_DesktopSortConfig.bgGradientColor1.ToCOLORREF());
	m_ColorBtn2.SetColor(s_DesktopSortConfig.bgGradientColor2.ToCOLORREF());

	m_ColorItemBtn1.SubclassWindow(GetDlgItem(IDC_BUTTON_COLOR_ITEM_1));
	m_ColorItemBtn2.SubclassWindow(GetDlgItem(IDC_BUTTON_COLOR_ITEM_2));

	m_FontName.Attach(GetDlgItem(IDC_STATIC_FONT));
	
	m_ColorItemBtn1.SetColor(s_DesktopSortConfig.itemGradientColor1.ToCOLORREF());
	m_ColorItemBtn2.SetColor(s_DesktopSortConfig.itemGradientColor2.ToCOLORREF());

	UpdateFontLabel(m_FontName, s_DesktopSortConfig.groupNamefont);	

	m_SliderOpacityGroup.SetRange(0,100);
	m_SliderOpacityBorder.SetRange(0,100);

	m_SliderOpacityGroup.SetPos((100 * s_DesktopSortConfig.groupOpacity) / 255);
	m_SliderOpacityBorder.SetPos((100 * s_DesktopSortConfig.groupBorderOpacity) / 255);	

	DoDataExchange(FALSE);

	EnableDisableButtons();

	return 0;
}

LRESULT CFrogSortDesktopDialog::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here
	DoDataExchange(TRUE);

	s_DesktopSortConfig.bgGradientColor1.SetFromCOLORREF(m_ColorBtn1.GetColor());
	s_DesktopSortConfig.bgGradientColor2.SetFromCOLORREF(m_ColorBtn2.GetColor());

	s_DesktopSortConfig.itemGradientColor1.SetFromCOLORREF(m_ColorItemBtn1.GetColor());
	s_DesktopSortConfig.itemGradientColor2.SetFromCOLORREF(m_ColorItemBtn2.GetColor());

	s_DesktopSortConfig.borderColor.SetFromCOLORREF(m_ColorBorderBtn.GetColor());

	s_DesktopSortConfig.useExistingWallaper = m_UseExistingWallaper;

	s_DesktopSortConfig.groupOpacity = 255 * m_SliderOpacityGroup.GetPos() / 100;
	s_DesktopSortConfig.groupBorderOpacity = 255 * m_SliderOpacityBorder.GetPos() / 100;

	EndDialog(wID);
	return 0;
}

LRESULT CFrogSortDesktopDialog::OnCheckUseWallpaper(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	DoDataExchange(TRUE);
	EnableDisableButtons();
	return 0;
}

void CFrogSortDesktopDialog::EnableDisableButtons()
{
	if (m_UseExistingWallaper)
	{
		::EnableWindow(GetDlgItem(IDC_BUTTON_COLOR_1), FALSE);
		::EnableWindow(GetDlgItem(IDC_BUTTON_COLOR_2), FALSE);

		::EnableWindow(GetDlgItem(IDC_STATIC_COLOR_1), FALSE);
		::EnableWindow(GetDlgItem(IDC_STATIC_COLOR_2), FALSE);
	}
	else
	{
		::EnableWindow(GetDlgItem(IDC_BUTTON_COLOR_1), TRUE);
		::EnableWindow(GetDlgItem(IDC_BUTTON_COLOR_2), TRUE);

		::EnableWindow(GetDlgItem(IDC_STATIC_COLOR_1), TRUE);
		::EnableWindow(GetDlgItem(IDC_STATIC_COLOR_2), TRUE);
	}
}
LRESULT CFrogSortDesktopDialog::OnButtonFont(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	GetSelectedFont(m_FontName);
	return 0;
}

void CFrogSortDesktopDialog::GetSelectedFont(CStatic& fontName)
{
	CFontDialog fontDialog;
	fontDialog.SetLogFont(&s_DesktopSortConfig.groupNamefont);
	fontDialog.m_cf.rgbColors = s_DesktopSortConfig.fontColor.ToCOLORREF();

	if (fontDialog.DoModal() == IDOK)
	{
		fontDialog.GetCurrentFont(&s_DesktopSortConfig.groupNamefont);
		UpdateFontLabel(fontName, s_DesktopSortConfig.groupNamefont);
		s_DesktopSortConfig.fontColor.SetFromCOLORREF(fontDialog.GetColor());
	}
}

void CFrogSortDesktopDialog::UpdateFontLabel(CStatic& fontName, const LOGFONTW& font)
{
	int fontSize = - MulDiv(font.lfHeight ,72, GetDeviceCaps(GetDC(), LOGPIXELSY));
	WCHAR buffer[1024];
	swprintf_s(buffer, 1024, L"%s, %dpt", font.lfFaceName, fontSize);
	fontName.SetWindowText(buffer);
}

LRESULT CFrogSortDesktopDialog::OnSliderOpacity1Change(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	HWND labelPercent = GetDlgItem(IDC_STATIC_PERECNT1);

	LRESULT lResult = m_SliderOpacityGroup.GetPos();

	wchar_t buffer[1024];
	_itow_s((int)lResult, buffer, 1024, 10);
	wstring label = buffer;
	label = label + L"%";

	::SetWindowText(labelPercent, label.c_str());

	return 0;
}

LRESULT CFrogSortDesktopDialog::OnSliderOpacity2Change(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	HWND labelPercent = GetDlgItem(IDC_STATIC_PERECNT2);

	LRESULT lResult = m_SliderOpacityBorder.GetPos();

	wchar_t buffer[1024];
	_itow_s((int)lResult, buffer, 1024, 10);
	wstring label = buffer;
	label = label + L"%";

	::SetWindowText(labelPercent, label.c_str());

	return 0;
}